﻿#include <cstdlib> // For std::putenv
#include <cstring> // For std::strdup
#include <qwidget.h>
#include <QHBoxLayout>
#include <QIcon>
#include <QVBoxLayout>
#include <iostream>
#include <windows.h>

#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/frame/XDesktop.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/Sequence.hxx>
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/uno/Any.hxx>
#include <com/sun/star/document/IndexedPropertyValues.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/sheet/XSpreadsheet.hpp>
#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/bridge/XUnoUrlResolver.hpp>
#include <com/sun/star/util/XCloseable.hpp>
#include <cppuhelper/bootstrap.hxx>

#include <osl/mutex.hxx>
#include <rtl/ustring.hxx>
#include <iostream>
#include <sal/main.h>
#include <osl/file.hxx>
#include <osl/process.h>
#include <rtl/process.h>
#include <rtl/ustrbuf.hxx>

#include "GenerateReportBase.h"
#include "AutomaticallySaveReport.h"

using namespace std;
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::bridge;
using namespace com::sun::star::frame;
using namespace com::sun::star::sheet;
using namespace com::sun::star::registry;
using namespace com::sun::star::text;
using namespace rtl;

using com::sun::star::beans::PropertyValue;

using com::sun::star::text::XTextDocument;
using com::sun::star::text::XText;
using com::sun::star::text::XTextCursor;

namespace GenerateReport
{
	AutomaticallySaveReport::AutomaticallySaveReport()
	{
	}
	//AutomaticallySaveReport::~AutomaticallySaveReport()
	//{
	//}

	void AutomaticallySaveReport::AutomaticallySaveReportExcute()
	{
		char tempPath[MAX_PATH];
		DWORD length = GetTempPathA(MAX_PATH, tempPath);
		if (!(length > 0))
		{
			std::cerr << "Failed to get temporary folder path" << std::endl;
		}

		std::string path = tempPath;
		std::string _fullPath = path;
		for (size_t i = 0; i < path.length(); ++i)
		{
			if (path[i] == '\\')
			{
				path[i] = '/';
			}
		}
		auto _currentReportName = GenerateReportBaseInstance->getCurrentReportName();
		_fullPath += _currentReportName.toStdString();
		_fullPath += ".docx";
		GenerateReportBaseInstance->setCurrentTempDocumentFilePath(_fullPath);
		auto xComponent = GenerateReportBaseInstance->getTextDocument();
		//临时保存
		Reference<com::sun::star::frame::XStorable> xStorable(xComponent, UNO_QUERY);
		if (xStorable.is())
		{
			// 创建保存属性集
			Sequence<PropertyValue> aMediaDescriptor(2);
			aMediaDescriptor[0].Name = "Overwrite";
			aMediaDescriptor[0].Value <<= sal_True;
			aMediaDescriptor[1].Name = "FilterName";
			aMediaDescriptor[1].Value <<= OUString("MS Word 2007 XML");

			OUString _outputPath = OUString("file:///") + OUString::fromUtf8(path.c_str()) + OUString::fromUtf8(_currentReportName.toStdString().c_str()) + OUString(".docx");
			try {
				xStorable->storeToURL(
					_outputPath,
					//OUString("file:///F:/LibreofficeTest/export1.docx"),
					aMediaDescriptor
				);
			}
			catch (Exception& e)
			{
				// 处理保存文档时可能出现的异常
				OUString errorMessage = e.Message;
				// 可以在这里输出错误信息或进行其他错误处理
			}
		}
	}

	void AutomaticallySaveReport::ScheduledSaveReport()
	{
	}

}
